﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.UI
{
  public partial class Header : UserControl
  {
    private Uri _documentUri;

    public event EventHandler SaveButtonClick
    {
      add { SaveButton.Click += value; }
      remove { SaveButton.Click -= value; }
    }

    public event EventHandler FullScreenButtonClick
    {
      add { FullScreenButton.Click += value; }
      remove { FullScreenButton.Click -= value; }
    }

    public event EventHandler PreviousPageButtonClick
    {
      add { PreviousPageButton.Click += value; }
      remove { PreviousPageButton.Click -= value; }
    }

    public event EventHandler NextPageButtonClick
    {
      add { NextPageButton.Click += value; }
      remove { NextPageButton.Click -= value; }
    }
    
    public Header()
    {
      InitializeComponent();
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      Rectangle.Width = finalSize.Width;
      Controls.Width = finalSize.Width;

      Logo.SetValue(Canvas.LeftProperty, finalSize.Width - 25);
      Title.SetValue(Canvas.LeftProperty, finalSize.Width - 140);

      PageSwitcherStackPanel.Arrange(LayoutUtils.CreateRect(LayoutUtils.Zero, PageSwitcherStackPanel.DesiredSize));

      if (_documentUri != null)
      {
        double availableTextWidth = Title.GetLocation().X - DocumentUriText.GetLocation().X - 10;
        DocumentUriText.Text = _documentUri.ToString();

        // document uri text is too large. trim it down until it fits or there is nothing left to trim
        while (DocumentUriText.ActualWidth > availableTextWidth && DocumentUriText.Text.Contains('/'))
        {
          string currentText = DocumentUriText.Text;
          // find last uri segment start/end
          int lastSlashIndex = currentText.LastIndexOf('/');
          int previousSlashIndex = currentText.Substring(0, lastSlashIndex).LastIndexOf('/');

          // segment is only 3 characters. find previous
          if (lastSlashIndex - previousSlashIndex <= 4)
            previousSlashIndex = currentText.Substring(0, previousSlashIndex).LastIndexOf('/');

          // replace segment with "..."
          // just show document name if not segments are left
          if (previousSlashIndex <= 6)
            DocumentUriText.Text = currentText.Substring(lastSlashIndex + 1);
          else
            DocumentUriText.Text = currentText.Substring(0, previousSlashIndex + 1) + @"..." + currentText.Substring(lastSlashIndex);
        }
      }
      else
      {
        DocumentUriText.Text = string.Empty;
      }

      return finalSize;
    }

    public void SetPageText(string s)
    {
      PageText.Text = s;
    }

    public void SetDocumentUriText(Uri documentUri)
    {
      _documentUri = documentUri;
      InvalidateArrange();
    }
  }
}
